import sql from '@/utils/sqlLite.js'
import {
	setObjSql,
	obj2str_sql
} from '@/utils/index.js'
// 数据表名称 代理商钱 表  代理商的在线金额 离线金额 营业额 押金 用户id 。。。
export const money = 'money'
export const database = uni.getStorageSync('database')
/*
customerAccount 代理商Id
onMoney 在线金额
 offMoney 离线金额
 turnover 今日营业额
*/
export const crateMoney = async () => {

	let sqltxt = [
		`create table if not exists ${money}(
		"customerAccount" VARCHAR(24),
		"onMoney" int(8),
		"offMoney" int(8),
		"turnover" int(8),
		"amountIncome" int(8),
		"amountTurnover" int(8),
		"amountDeposit" int(8))`
	];
	let res = await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
	console.log(res);
}

export const addOrUpdateMoney = async (pushData) => {

	// 数据库中customerAccount 存在 修改 ，不存在 新增
	let sqltxt = `select *from ${money} where customerAccount = '${pushData.customerAccount}'`;
	let res = await sql.selectSQL(uni.getStorageSync('database'), sqltxt);

	if (res.code == 0 && res.data.length == 0) { //数据库中没有数据 新增

		let condition = Object.keys(pushData).join(',')
		let data = setObjSql(pushData)
		let sqltxt = `INSERT  INTO ${money} (${condition}) VALUES(${data})`
		return await sql.executeSQL(uni.getStorageSync('database'), sqltxt);

	} else { //存在 ，更改
		let data = obj2str_sql(pushData)
		let sqltxt = `update  ${money} set ${data} where customerAccount = '${pushData.customerAccount}'`
		let rrs = await sql.executeSQL(uni.getStorageSync('database'), sqltxt);
		console.log(rrs);
	}
}


// 获取pid对应的设备详情
export const getMoneyInfo = async () => {
	let sqltxt = `select * from ${money} `;
	return await sql.selectSQL(uni.getStorageSync('database'), sqltxt);
}